<!DOCTYPE html>  
<html>
<meta charset="utf-8" />  
<title></title>   
<body>
    <div></div>
</body>
<script>
    // 方法一
    // /*
    // String.prototype.render = function(obj) {
    //     return this.replace(/\$\{(\w+|\w+\.\w+)\}/g, match => {
    //         var keys = match.replace('${', '').replace('}', '').split('.')
    //         return keys.reduce((acc, cv) => acc[cv], obj)
    //     })
    // }
    // */
    
    // 方法二
    // /*
    String.prototype.render = function (obj) {
        with(obj) {
            return eval('`' + this + '`')
        }
    }
    // */

    var greeting = 'My name is ${name}, age ${age}, I am a ${job.jobName}';
    var employee = {
        name: 'XiaoMing',
        age: 11,
        job: {
            jobName: 'designer',
            jobLevel: 'senior'
        } 
    };
    var result = greeting.render(employee);
    console.log(result);//打印的结果为My name is XiaoMing, age 11, I am a designer

</script>
</html>